<?php
namespace app\admin\model;
use think\Config;
use think\Db;
class UserModel extends \app\common\model\CommonModel
{

    /**
     * @param string $company    公司
     * @param string $username   用户姓名
     * @param int    $pagesize   页码大小
     * @param int    $page       页码
     * @return array
     */
    public function list($params)
    {
        try {
            $page     = isset($params['page']) && is_numeric($params['page']) ? (int)$params['page'] : 1;
            $pagesize = isset($params['pagesize']) && is_numeric($params['pagesize']) ? (int)$params['pagesize'] : 10;
            $start = ($page-1) * $pagesize;
            $field = "username,userid,post,mail,department,company,active_time,last_login,departmentId,postId";
            $sql = "SELECT {$field} FROM user WHERE 1=1 ";
            if(!empty($params['company'])){
                $sql .= "AND company LIKE '%{$params['company']}%' ";
            }
            if(!empty($params['username'])){
                $sql .= "AND username LIKE '%{$params['username']}%' ";
            }
            $order = "ORDER BY create_time DESC LIMIT {$start},{$pagesize}";
            $csql = str_replace($field,'count(*)',$sql);
            $sql .= $order;
            $res = $this->getRes($sql);
            $count = $this->getCount($csql);
            return $this->pageData($res,$pagesize,$page,$count);
        } catch (\Exception $e) {
            throw new \Exception($e->getMessage());
        }
    }
    
}